package com.fastshopping.order.controller;

import com.fastshopping.order.pojo.Order;
import com.fastshopping.order.service.OrderService;
import com.github.pagehelper.Page;
import com.wink.entity.PageResult;
import com.wink.entity.Result;
import com.wink.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
 * @Author zhangdan
 * @Description //TODO 订单管理
 * @Date 17:18 2020/6/1
 * @Param 
 * @return 
 **/
@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    /*
     * @Author zhangdan
     * @Description //TODO 查询全部数据
     * @Date 17:23 2020/6/7
     * @Param []
     * @return com.wink.entity.Result
     **/
    @GetMapping
    public Result findAll(){
        List<Order> orderList = orderService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",orderList) ;
    }

    /*
     * @Author zhangdan
     * @Description //TODO 根据ID查询数据
     * @Date 17:23 2020/6/7
     * @Param [id]
     * @return com.wink.entity.Result
     **/
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Order order = orderService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",order);
    }


    /*
     * @Author zhangdan
     * @Description //TODO 新增数据
     * @Date 17:23 2020/6/7
     * @Param [order]
     * @return com.wink.entity.Result
     **/
    @PostMapping
    public Result add(@RequestBody Order order){
        orderService.add(order);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /*
     * @Author zhangdan
     * @Description //TODO 修改数据
     * @Date 17:23 2020/6/7
     * @Param [order, id]
     * @return com.wink.entity.Result
     **/
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Order order,@PathVariable int id){
        order.setId(id);
        orderService.update(order);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /*
     * @Author zhangdan
     * @Description //TODO 根据ID删除品牌数据
     * @Date 17:23 2020/6/7
     * @Param [id]
     * @return com.wink.entity.Result
     **/
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        orderService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /*
     * @Author zhangdan
     * @Description //TODO 多条件搜索品牌数据
     * @Date 17:24 2020/6/7
     * @Param [searchMap]
     * @return com.wink.entity.Result
     **/
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Order> list = orderService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /*
     * @Author zhangdan
     * @Description //TODO 分页搜索实现
     * @Date 17:24 2020/6/7
     * @Param [searchMap, page, size]
     * @return com.wink.entity.Result
     **/
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Order> pageList = orderService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


}
